package org.an.proj.contacts.ui.contact.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import org.an.ku.view.dialog.chose.ChooseDialogAdapter;
import org.an.ku.view.dialog.chose.ChooseDialogInfo;
import org.an.proj.contacts.R;

/**
 * Created by Fcy on 2016/8/18.
 * 添加更多属性的Dialog
 */
public class MoreAttrsDialog extends Dialog implements AdapterView.OnItemClickListener {
  protected Context mContext;
  private TextView mtvDialogTitle;
  private ListView mListView;

  private String mDialogTitle;
  private List<ChooseDialogInfo> mInfos = new ArrayList<>();
  private ChooseDialogAdapter mAdapter;

  // item最大可见个数，默认为wrap
  private int mMaxItemCount = -1;

  // 请指定为 selector color,并设置selected区分选中与否的颜色
  private int mColorId = ChooseDialogAdapter.DEFAULT_COLOR;

  private OnItemClickListener mOnItemClickListener;

  /**
   * @param title 标题
   */
  public MoreAttrsDialog(Context context, String title) {
    super(context, R.style.dialog_bottom);
    this.mContext = context;
    this.mDialogTitle = title;
  }

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dialog_moreattrs);
    // 设置点击返回键消失，如果为false，返回键将无响应
    setCancelable(true);
    getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,
      WindowManager.LayoutParams.WRAP_CONTENT);
    getWindow().setGravity(Gravity.BOTTOM);
    // 初始化dialog信息
    initView();
  }

  private void initView() {
    mtvDialogTitle = (TextView) findViewById(R.id.dialog_chose_title);
    mtvDialogTitle.setText(mDialogTitle);
    mListView = (ListView) findViewById(R.id.dialog_chose_list);
    mAdapter = new ChooseDialogAdapter(mContext, mColorId);
    mListView.setAdapter(mAdapter);
    mListView.setOnItemClickListener(this);
    checkHeight();
    refresh();
  }

  /**
   * 在onDraw()之前会先调这个方法，所以会在show之前回调，可以处理
   */
  @Override public void onAttachedToWindow() {
    mListView.setSelection(0);
  }

  public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
    this.mOnItemClickListener = onItemClickListener;
  }

  public MoreAttrsDialog setItemTextColorId(int colorId) {
    this.mColorId = colorId;
    return this;
  }

  public MoreAttrsDialog setTitle(String title) {
    this.mDialogTitle = title;
    return this;
  }

  /**
   * 不限制高度
   */
  public MoreAttrsDialog setItemCountUnLimit() {
    return setMaxItemCount(-1);
  }

  /**
   * 设置listview最多显示的item个数
   */
  public MoreAttrsDialog setMaxItemCount(int maxItemCount) {
    this.mMaxItemCount = maxItemCount;
    return this;
  }

  /**
   * @param list
   */
  public MoreAttrsDialog setData(List<ChooseDialogInfo> list) {
    mInfos = list;
    return this;
  }

  private void checkHeight() {
    if (mInfos == null) return;

    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mListView.getLayoutParams();
    if (mMaxItemCount == -1 || mInfos.size() <= mMaxItemCount) {
      params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
    } else {
      params.height = mMaxItemCount * mContext.getResources()
        .getDimensionPixelOffset(R.dimen.dialog_chose_item_height);
    }
  }

  private void refresh() {
    mAdapter.setDatas(mInfos);
  }

  /**
   * dialog中item的点击事件
   */
  @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    if (mOnItemClickListener != null) {
      mOnItemClickListener.onItemClick(position, mInfos.get(position));
    }
    refresh();
  }

  /**
   * 单选
   */
  public interface OnItemClickListener {
    /**
     * @param position 列表下标
     * @param info 点击position处的info
     */
    void onItemClick(int position, ChooseDialogInfo info);
  }
}
